﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using NUnit.Framework;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace UnitTestTimesheet
{
    [TestFixture]
    public class TestTimesheetLogic
    {
        [Test]
        public void TestInsertTimesheet_Inputtimeisgreaterthan24_OutputFail()
        {
            var timesheetDetailEntity = new TimesheetDetailEntity
            {
                OnDate = DateTime.Parse("10/10/2012"),
                ContractNumber = "02/12/ContractMH",
                Time = 25,
                Description = "Coding"
            };
            string actual = TimesheetLogic.InsertTimesheet(timesheetDetailEntity);
            Assert.AreEqual(null, actual);
        }
        [Test]
        public void TestInsertTimesheet_InputTimeLessThan1_OuputError()
        {
            var timesheet = new TimesheetDetailEntity
            {
                OnDate = DateTime.Parse("10/10/2012"),
                ContractNumber = "02/12/ContractMH",
                Time = -1,
                Description = "Coding"
            };
            string actual = TimesheetLogic.InsertTimesheet(timesheet);
            Assert.AreEqual(null, actual);
        }

        [Test]
        public void TestInsertTimesheet_InputTimetimelessthan24andgreaterthan1_Outputsuccessful()
        {
            var timesheetDetailEntity = new TimesheetDetailEntity
            {
                OnDate = DateTime.Parse("10/10/2012"),
                ContractNumber = "02/12/ContractMH",
                Time = 8,
                Description = "Coding"
            };
            var actual = TimesheetLogic.InsertTimesheet(timesheetDetailEntity);
            Assert.AreEqual("Successfully!", actual);
        }

        [Test]
        public void TestInsertTimesheet_InputTimetimelessthan24_Outputsuccessfull()
        {
            var timesheet = new TimesheetDetailEntity
            {
                OnDate = DateTime.Parse("10/10/2012"),
                ContractNumber = "02/12/ContractMH",
                Time = 8,
                Description = "Coding"
            };
            var actual = TimesheetLogic.InsertTimesheet(timesheet);
            Assert.AreEqual("Successfully!", actual);
        }


        [Test]
        public void TestInsertTimesheet_InputTimegreaterthan1_OutputSuccessfully()
        {
            var timesheet = new TimesheetDetailEntity
            {
                OnDate = DateTime.Parse("10/10/2012"),
                ContractNumber = "02/12/ContractMH",
                Time = 8,
                Description = "Coding"
            };
            var actual = TimesheetLogic.InsertTimesheet(timesheet);
            Assert.AreEqual("Successfully!", actual);
        }



        [Test]
        public void TestInsertTimesheet_InputDescription_OutputError()
        {
            var timesheet = new TimesheetDetailEntity
                                {
                                    OnDate = DateTime.Parse("10/10/2012"),
                                    ContractNumber = "02/12/ContractMH",
                                    Time = 8,
                                    Status = "Pending",
                                    Description = ""
                                };
            var actual = TimesheetLogic.InsertTimesheet(timesheet);
            Assert.AreEqual("Successfully!", actual);
        }
        [Test]
        public void TestUpdateTimesheet_InputInfomationTimesheet_OutputTimesheet()
        {
            var timesheet = new TimesheetDetailEntity
            {
                OnDate = DateTime.Parse("10/10/2012"),
                ContractNumber = "02/12/ContractMH",
                Time = 8,
                Status = "Approve",
                Description = ""
            };
            var actual = TimesheetLogic.UpdateTimesheet(timesheet);
            Assert.AreEqual("Successfully!", actual);
        }

        //[Test]
        //public void TestUpdate_InputTimesheetEntityWithTimesheetIdDoNotExist_Return0()
        //{
        //    var timesheet = new TimesheetDetailEntity();
        //    timesheet.TimesheetDetailId = 6;
        //    timesheet.ContractNumber ="58";
        //    timesheet.Description = "Update";
        //    timesheet.Time = 4;
        //    timesheet.OnDate = DateTime.Now;
        //    var actualValue = TimesheetLogic.UpdateTimesheet(timesheet);
        //    const int expectedValue = 0;
        //    Assert.AreEqual(expectedValue, actualValue);
        //}

        [Test]
        public void TestApproveTimesheet_InputInfomationTimesheet_OutputApprove()
        {
            var timesheet = new TimesheetDetailEntity
            {
                OnDate = DateTime.Parse("10/20/2012"),
                ContractNumber = "02/12/ContractMH",
                Time = 8,
                Description = "Coding"
            };
            var actual = TimesheetLogic.ApproveTimesheet(timesheet);
            Assert.AreEqual("Successfully!", actual);
        }

        [Test]
        public void TestDeleteTimesheet_TimesheetInput_OutputDeleteSuccessfully()
        {
            var timesheet = new TimesheetDetailEntity
            {
                TimesheetDetailId = 1
            };
            var actual = TimesheetLogic.DeleteTimesheet(timesheet);
            Assert.AreEqual("Successfully!", actual);
        }

        [Test]
        public void TestSelectTimesheetDetailByTimesheetDetailId_InputTimesheetId_OutputTimesheet()
        {
            var expected = new DataTable();
            expected.Columns.Add("TimesheetId");
            expected.Columns.Add("ContractNumber");
            expected.Columns.Add("OnDate");
            expected.Columns.Add("Time");
            expected.Columns.Add("Description");
            expected.Columns.Add("Status");
            var row = expected.NewRow();
            row["TimesheetId"] = "";
            row["ContractNumber"] = "";
            row["OnDate"] = "";
            row["Time"] = "";
            row["Description"] = "";
            row["Status"] = "";
            expected.Rows.Add(row);
            const string timesheetId = "2";
            var actual = TimesheetLogic.SelectTimesheetDetailByTimesheetDetailId(timesheetId);
            Assert.AreEqual(false, CompareTable(expected, actual));
        }

        [Test]
        public void TestSelectDataTimesheetByClient_InputClient_OutpuntTimesheet()
        {
            const string user = "HenterJ";
            var expected = GetExcelData("TimesheetOfClient");
            var actual = TimesheetLogic.SelectDataTimesheetByClient(user);
            Assert.AreEqual(true, CompareTable(expected, actual));
        }

        [Test]
        public void TestSelectDataTimesheetByConsultant_InputConsultant_OutputListTimesheet()
        {        
            const string user = "hienttm";
            var expected = GetExcelData("TimesheetOfConsultant");
            var actual = TimesheetLogic.SelectDataTimesheetByConsultant(user);
            Assert.AreEqual(false, CompareTable(expected, actual));
        }

        [Test]
        public void TestSearchTimesheet_InputInfomationOfTimesheet_OutputListTimesheet()
        {
            var expected = new DataTable();
            expected.Columns.Add("TimesheetDetailId");
            expected.Columns.Add("OnDate");
            expected.Columns.Add("Time");
            expected.Columns.Add("Description");
            expected.Columns.Add("Status");
            var row = expected.NewRow();
            row["TimesheetDetailId"] = "";
            row["OnDate"] = "";
            row["Time"] = "";
            row["Description"] = "";
            row["Status"] = "";
            const string contractNumber = "03/12/ContractBG";
            const string timesheetStatus = "Pending";
            const string fromDate = "7/12/2012 12:00:00 AM";
            const string toDate = "7/20/2012 12:00:00 AM";
            const string sort = "Date";
            var actual = TimesheetLogic.SearchTimesheet(contractNumber, timesheetStatus, fromDate, toDate, sort);
            Assert.AreEqual(true, CompareTable(expected, actual));

        }

        [Test]
        public void TestSearchTimesheetByConsultant_InputInfomationTimesheetandUserNameConsultant_OutputListTimesheet()
        {
            var expected = new DataTable();
            expected.Columns.Add("TimesheetDetailId");
            expected.Columns.Add("OnDate");
            expected.Columns.Add("Time");
            expected.Columns.Add("Description");
            expected.Columns.Add("Status");
            var row = expected.NewRow();
            row["TimesheetDetailId"] = "";
            row["OnDate"] = "";
            row["Time"] = "";
            row["Description"] = "";
            row["Status"] = "";
            const string contractNumber = "";
            const string timesheetStatus = "";
            const string fromDate = "";
            const string toDate = "";
            const string sort = "";
            const string userName = "hienttm";
           // var expected = GetExcelData("TimesheetOfConsultant");
            var actual = TimesheetLogic.SearchTimesheetByConsultant(contractNumber, timesheetStatus, fromDate, toDate, sort, userName);
            Assert.AreEqual(true, CompareTable(expected, actual));
        }

        [Test]
        public void TestSortTimesheet_InputSortBy_OutputTimesheet()
        {
            var expected = new DataTable();
            expected.Columns.Add("TimesheetDetailId");
            expected.Columns.Add("OnDate");
            expected.Columns.Add("Time");
            expected.Columns.Add("Description");
            expected.Columns.Add("Status");
            var row = expected.NewRow();
            row["TimesheetDetailId"] = "";
            row["OnDate"] = "";
            row["Time"] = "";
            row["Description"] = "";
            row["Status"] = "";
            const string sortBy = "TimesheetDetailId";
            var actual = TimesheetLogic.SortTimesheet(sortBy);
            Assert.AreEqual(true, CompareTable(expected, actual));
        }

        [Test]
        public void TestFilterApproveTimesheet_InputInfomationContractNumberAndStatus_OutputListTimesheethasApprove()
        {
            var timesheet = new TimesheetDetailEntity
            {
                Status = "Approve"
            };
            var expected = new DataTable();
            expected.Columns.Add("TimesheetDetailId");
            expected.Columns.Add("OnDate");
            expected.Columns.Add("Time");
            expected.Columns.Add("Description");
            expected.Columns.Add("Status");
            var row = expected.NewRow();
            row["TimesheetDetailId"] = "";
            row["OnDate"] = "";
            row["Time"] = "";
            row["Description"] = "";
            row["Status"] = "";

            var actual = TimesheetLogic.FilterApproveTimesheet(timesheet);
            Assert.AreEqual(true, CompareTable(expected, actual));
        }

        [Test]
        public void TestFilterApproveTimesheetByClient_InputClientAndStatus_OutputListTimesheethasApprove()
        {
            var timesheet = new TimesheetDetailEntity
            {
                Status = "Approve"
            };
            var expected = new DataTable();
            expected.Columns.Add("TimesheetDetailId");
            expected.Columns.Add("OnDate");
            expected.Columns.Add("Time");
            expected.Columns.Add("Description");
            expected.Columns.Add("Status");
            var row = expected.NewRow();
            row["TimesheetDetailId"] = "";
            row["OnDate"] = "";
            row["Time"] = "";
            row["Description"] = "";
            row["Status"] = "";
            const string userName = "tanlv";
            var actual = TimesheetLogic.FilterApproveTimesheetByClient(userName, timesheet);
            Assert.AreEqual(true, CompareTable(expected, actual));
        }

        public bool CompareTable(DataTable expect, DataTable actual)
        {
            var result = true;
            for (var i = 0; i < expect.Rows.Count; i++)
            {
                if (expect.Columns.Cast<object>().Where((t, j) => !expect.Rows[i][j].ToString().Equals(actual.Rows[i][j].ToString())).Any())
                {
                    result = false;
                }
            }
            return result;
        }

        [Test]
        public void TestSearchTimesheetByUserName_InputUserName_OutputTimesheet()
        {
            var timesheet = new TimesheetDetailEntity
                                   {
                                        OnDate = Convert.ToDateTime("09/05/2012"),
                                        Time = 8,
                                        ContractNumber = "274a8866-1050-40ef-857d-69d6742cb940",
                                        Description = "a5df4224-dd25-4125-9098-88637e7bdc40",
                                        Status = "All"
                                   };


            const string contractNumber = "";
            const string timesheetStatus = "";
            const string fromDate = "";
            const string toDate = "";
            const string sort = "";
            const string userName = "";
            var actualValue = TimesheetLogic.SearchTimesheetByUserName(contractNumber, timesheetStatus, fromDate, toDate, sort, userName).Rows.Count;
            Assert.True(actualValue == 0);
        }

        //[Test]
        //public void TestTimesheetSearch_InputParameterWithFromDateLestThanToDate_ReturnDataTableEmpty()
        //{
        //    const string fromDate = "09/05/2012";
        //    const string toDate = "08/05/2012";
        //    const string contractNumber = "274a8866-1050-40ef-857d-69d6742cb940";
        //    const string sort = "date";
        //    const string status = "All";
        //    var timesheet = new TimesheetDetailEntity();
        //    var actualValue = TimesheetLogic.SearchTimesheet(fromDate, toDate, contractNumber, status, sort).Rows.Count;
        //    Assert.True(actualValue == 0);
        //}

        [Test]
        public void TestUpdateReason_InputNull_OutputUnSuccessfully()
        {
            var timesheet = new TimesheetDetailEntity();
            {

            }
            ;
            var actual = TimesheetLogic.UpdateReason(timesheet);
            var expected = new DataTable();
            expected.Columns.Add("TimesheetDetailId");
            expected.Columns.Add("OnDate");
            expected.Columns.Add("Time");
            expected.Columns.Add("Description");
            expected.Columns.Add("Status");
            var row = expected.NewRow();
            row["TimesheetDetailId"] = "";
            row["OnDate"] = "";
            row["Time"] = "";
            row["Description"] = "";
            row["Status"] = "";
        }

        [Test]
        public void TestSelectReasonById_InputTimesheetId_OutputDescriptionReason()
        {
            var timesheet = new TimesheetDetailEntity
            {
                TimesheetDetailId = 2
            };
            var expected = new DataTable();
            expected.Columns.Add("TimesheetDetailId");
            expected.Columns.Add("OnDate");
            expected.Columns.Add("Time");
            expected.Columns.Add("Description");
            expected.Columns.Add("Status");
            var row = expected.NewRow();
            row["TimesheetDetailId"] = "";
            row["OnDate"] = "";
            row["Time"] = "";
            row["Description"] = "";
            row["Status"] = "";

        }

        [Test]
        public void TestSelectDataTimesheet()
        {
            var expect = GetExcelData("ListTimesheet");
            var actual = TimesheetLogic.SelectDataTimesheet();
            Assert.AreEqual(false, CompareTable(expect, actual));
        }

        public static DataTable GetExcelData(string sheetName)
        {
            const string fileName = "DataTest.xlsx";
            var conn = new OleDbConnection();
            var adapter = new OleDbDataAdapter();
            var dt = new DataTable();
            var command = conn.CreateCommand();
            const string excelFile = fileName;
            var excelType = Path.GetExtension(excelFile);
            string connstr;

            if (excelType == "XLSX")
                connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=Excel 8.0";
            else
                connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;IMEX=1; HDR=YES'";

            conn.ConnectionString = connstr;
            var sql = "SELECT * FROM [" + sheetName + "$]";

            command.CommandText = sql;
            adapter.SelectCommand = command;
            adapter.Fill(dt);

            return dt;
        }

      
    }
}
